package com.yuyang.wifi.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.widget.RelativeLayout;

import com.baidu.mobads.sdk.api.CPUWebAdRequestParam;
import com.baidu.mobads.sdk.api.CpuAdView;
import com.baidu.mobads.sdk.api.CpuLpFontSize;
import com.yuyang.wifi.R;
import com.yuyang.wifi.base.BaseFragment;
import com.yuyang.wifi.constant.AppConstant;
import com.yuyang.wifi.constant.ConstantsMsg;
import com.yuyang.wifi.utils.SpUtil;
import com.yuyang.wifi.utils.eventbus.BaseEvent;

import java.util.UUID;

import androidx.annotation.Nullable;
import butterknife.BindView;


/**
 * baidu热点
 */

public class BaiduAdTabFragment extends BaseFragment {

    @BindView(R.id.rl_contain_layout)
    RelativeLayout rlContainLayout;

    private CpuLpFontSize mDefaultCpuLpFontSize = CpuLpFontSize.REGULAR;
    private boolean isDarkMode = false;
    private CpuAdView mCpuView;
    public static final String TAG = "CpuAdActivity";
    private static final String ARG_ID = "classify_id";
    private int mAdType;

    public static BaiduAdTabFragment newInstance(String title) {
        BaiduAdTabFragment fragment = new BaiduAdTabFragment();
        Bundle bundle = new Bundle();
        bundle.putString(ARG_ID, title);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String title = getArguments().getString(ARG_ID);
        // "热点", "热榜", "娱乐", "体育", "手机", "财经", "汽车", "房产", "本地", "图片"
        switch (title) {
            case "热点":
                mAdType = 1021;
                break;
            case "热榜":
                mAdType = 1090;
                break;
            case "娱乐":
                mAdType = 1001;
                break;
            case "体育":
                mAdType = 1002;
                break;
            case "手机":
                mAdType = 1005;
                break;
            case "财经":
                mAdType = 1006;
                break;
            case "汽车":
                mAdType = 1007;
                break;
            case "房产":
                mAdType = 1008;
                break;
            case "本地":
                mAdType = 1080;
                break;
            case "图片":
                mAdType = 1003;
                break;
        }
    }


    @Override
    protected void initActionBar() {

    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_tab_baidu_ad_page;
    }

    @Override
    protected void initBizView() {
        showSelectedCpuWebPage();
    }

    /**
     * 内容联盟模板渲染，展示频道
     */
    private void showSelectedCpuWebPage() {

        /**
         *  注意构建参数时，setCustomUserId 为必选项，
         *  传入的outerId是为了更好的保证能够获取到广告和内容
         *  outerId的格式要求： 包含数字与字母的16位 任意字符串
         */

        /**
         *  推荐的outerId获取方式：
         */
        String outerId = SpUtil.find(AppConstant.KEY_OUTERID);
        if (TextUtils.isEmpty(outerId)) {
            outerId = UUID.randomUUID().toString()
                    .replace("-", "")
                    .substring(0, 16);
            SpUtil.saveOrUpdate(AppConstant.KEY_OUTERID, outerId);
        }

        CPUWebAdRequestParam cpuWebAdRequestParam = new CPUWebAdRequestParam.Builder()
                .setLpFontSize(mDefaultCpuLpFontSize)
                .setLpDarkMode(isDarkMode)
                .setCityIfLocalChannel("北京")
                .setCustomUserId(outerId)
                .build();

        mCpuView = new CpuAdView(getActivity(), ConstantsMsg.KS_BAIDU_APP_SID, mAdType, cpuWebAdRequestParam,
                new CpuAdView.CpuAdViewInternalStatusListener() {
                    @Override
                    public void loadDataError(String message) {
                        Log.d(TAG, "loadDataError: " + message);
                    }

                    @Override
                    public void onAdClick() {
                        Log.d(TAG, "onAdClick: ");
                    }

                    @Override
                    public void onAdImpression(String impressionAdNums) {
                        Log.d(TAG, "onAdImpression: impressionAdNums " + impressionAdNums);
                    }

                    @Override
                    public void onContentClick() {
                        Log.d(TAG, "onContentClick: ");
                    }

                    @Override
                    public void onContentImpression(String impressionContentNums) {
                        Log.d(TAG, "onContentImpression: impressionContentNums = " +
                                impressionContentNums);
                    }
                });

        // 考虑到媒体在 锁屏开关等场景下 有刷新页面的需求，SDK目前将控件的创建与
        // 数据的请求进行拆分，媒体在已创建控件的前提下，可以直接调用下面这行代码来请求或着更新数据
        mCpuView.requestData();
        RelativeLayout.LayoutParams reLayoutParams = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        reLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
        rlContainLayout.addView(mCpuView, reLayoutParams);
    }


    @Override
    public void onResume() {
        super.onResume();
        if (mCpuView != null) {
            mCpuView.onResume();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (mCpuView != null) {
            mCpuView.onPause();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mCpuView != null) {
            mCpuView.onDestroy();
        }
    }
}
